package com.zzh.demo.core.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * description: <br>
 * createTime: 2018/8/1614:57 <br>
 *
 * @author zzh
 */
@Configuration    // 配置注解，自动在本类上下文加载一些环境变量信息
@EnableSwagger2   // 使swagger2生效
//@EnableWebMvc
@Profile("debug")
@ComponentScan(basePackages = {"com.zzh.demo.**.controller"})  //需要扫描的包路径
public class SwaggerConfig {

    public SwaggerConfig() {
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");
    }


    @Bean
    public Docket swaggerSpringMvcPlugin() {
        System.out.println("over");
        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
                .groupName("business-api")
                .select()   // 选择那些路径和api会生成document
//                .apis(RequestHandlerSelectors.basePackage("com.custom.web"))
//                .paths(paths())
                .apis(RequestHandlerSelectors.any())  // 对所有api进行监控
                .paths(PathSelectors.any())   // 对所有路径进行监控
                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts())
                ;
    }

//    private Predicate<String> paths() {
//        return Predicates.or(regex("/person.*"));
//    }

//    private List<ApiKey> securitySchemes() {
//        return newArrayList(
//                new ApiKey("clientId", "客户端ID", "header"),
//                new ApiKey("clientSecret", "客户端秘钥", "header"),
//                new ApiKey("accessToken", "客户端访问标识", "header"));
//    }

//    private List<SecurityContext> securityContexts() {
//        return newArrayList(
//                SecurityContext.builder()
//                        .securityReferences(defaultAuth())
//                        .forPaths(regex("/*.*"))
//                        .build()
//        );
//    }

//    List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return newArrayList(
//                new SecurityReference("clientId", authorizationScopes),
//                new SecurityReference("clientSecret", authorizationScopes),
//                new SecurityReference("accessToken", authorizationScopes));
//    }

//    private ApiInfo apiInfo() {
//        return new ApiInfoBuilder()
//                .title("Spring 中使用Swagger2构建RESTful API")
//                .termsOfServiceUrl("http://blog.csdn.net/yangshijin1988")
//                .description("此API提供接口调用")
//                .license("License Version 2.0")
//                .licenseUrl("http://blog.csdn.net/yangshijin1988")
//                .version("2.0").build();
//    }
}